package com.cmict.data.bpm.dal.mysql.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.bpm.controller.admin.task.vo.instance.BpmProcessInstanceMyReqVO;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.query.QueryWrapperX;
import com.cmict.data.bpm.controller.admin.task.vo.instance.BpmProcessInstanceMyPageReqVO;
import com.cmict.data.bpm.controller.admin.task.vo.instance.BpmProcessInstancePageItemRespVO;
import com.cmict.data.bpm.dal.dataobject.task.BpmProcessInstanceExtDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Mapper
public interface BpmProcessInstanceExtMapper extends BaseMapperX<BpmProcessInstanceExtDO> {

    default PageResult<BpmProcessInstanceExtDO> selectPage(Long userId, BpmProcessInstanceMyPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BpmProcessInstanceExtDO>()
                .eqIfPresent(BpmProcessInstanceExtDO::getStartUserId, userId)
                .likeIfPresent(BpmProcessInstanceExtDO::getName, reqVO.getName())
                .eqIfPresent(BpmProcessInstanceExtDO::getProcessDefinitionId, reqVO.getProcessDefinitionId())
                .eqIfPresent(BpmProcessInstanceExtDO::getCategory, reqVO.getCategory())
                .eqIfPresent(BpmProcessInstanceExtDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BpmProcessInstanceExtDO::getResult, reqVO.getResult())
                .betweenIfPresent(BpmProcessInstanceExtDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(BpmProcessInstanceExtDO::getId));
    }

    default BpmProcessInstanceExtDO selectByProcessInstanceId(String processInstanceId) {
        return selectOne("process_instance_id", processInstanceId);
    }

    default void updateByProcessInstanceId(BpmProcessInstanceExtDO updateObj) {
        update(updateObj, new QueryWrapper<BpmProcessInstanceExtDO>()
                .eq("process_instance_id", updateObj.getProcessInstanceId()));
    }

    List<BpmProcessInstancePageItemRespVO> selectPageProcessInstance(@Param("userId")Long userId, @Param("pageReqVO") BpmProcessInstanceMyPageReqVO pageReqVO);

    List<BpmProcessInstancePageItemRespVO> selectProcessInstanceNoPage(@Param("userId")Long userId, @Param("reqVO") BpmProcessInstanceMyReqVO reqVO);

    Long selectPageProcessInstanceCount(@Param("userId")Long userId, @Param("pageReqVO") BpmProcessInstanceMyPageReqVO pageReqVO);

    @Update("update `bpm_process_instance_ext` set deleted = 1 where process_instance_id in (#{instancesIds})")
    void deleteByProcessInstanceIds(@Param("instancesIds") String instancesIds);

    default List<BpmProcessInstanceExtDO> selectByProcessDefinitionId(Set<String> processDefinitionIdSet) {
        return selectList(new QueryWrapperX<BpmProcessInstanceExtDO>()
                .inIfPresent("process_definition_id",processDefinitionIdSet)
                .orderByDesc("update_time"));
    }

    default List<BpmProcessInstanceExtDO> selectByProcessDefinitionIdData(String id ,String processDefinitionId) {
            return selectList(new QueryWrapperX<BpmProcessInstanceExtDO>()
                    .eq("process_definition_id",processDefinitionId)
                    .eq("form_record_id",id)
                    .orderByDesc("create_time"));
    }
    @Select("${sql}")
        //@DS("slave")
    Map<String, Object> getSysFormDataById(String sql);
}
